package fl.ass.service.api;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.constant.DictionaryConstant;
import fl.ass.facade.api.IAssVfInfoApi;
import fl.ass.facade.constant.AssVfInfoConstant;
import fl.ass.facade.dto.AssVfInfoDTO;
import fl.ass.facade.dto.AssVfRtnLogDTO;
import fl.ass.service.service.IAssVfInfoService;
import fl.ass.service.service.IAssVfRtnLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: huangkailun
 * @Date: 2020-11-27 16:34
 */
@Service
public class AssVfInfoApi implements IAssVfInfoApi {

    @Autowired
    private IAssVfInfoService assVfInfoService;

    @Autowired
    private ISysDictionaryApiService dictionaryApiService;

    @Autowired
    private IAssVfRtnLogService assVfRtnLogService;

    @Override
    public Pager searchAssVfInfoListPage(Pager page) throws BaseException {
        Pager pager = this.assVfInfoService.searchListPage(page);
        List<AssVfInfoDTO> resultList = (List<AssVfInfoDTO>) pager.getResultList();
        if (CollectionUtils.isNotEmpty(resultList)) {
            Map<String, String> vfStsCdMap = dictionaryApiService.searchGroupToMap(AssVfInfoConstant.SYS_CD, AssVfInfoConstant.ORG_CD, AssVfInfoConstant.VF_STS_GRP_CD);
            for (AssVfInfoDTO assVfInfoDTO : resultList) {
                if (vfStsCdMap != null && vfStsCdMap.containsKey(assVfInfoDTO.getVfStsCd())) {
                    assVfInfoDTO.setVfStsCdNm(vfStsCdMap.get(assVfInfoDTO.getVfStsCd()));
                }
            }
            Map<String, String> prdStsCdMap = dictionaryApiService.searchGroupToMap(AssVfInfoConstant.SYS_CD, AssVfInfoConstant.ORG_CD, DictionaryConstant.CODE_PRJ_PRD_STS_CD);
            for (AssVfInfoDTO assVfInfoDTO : resultList) {
                if (prdStsCdMap != null && prdStsCdMap.containsKey(assVfInfoDTO.getPaySchStsCd())) {
                    assVfInfoDTO.setPrdStsCdNm(prdStsCdMap.get(assVfInfoDTO.getPaySchStsCd()));
                }
            }
            pager.setResultList(resultList);
        }
        return pager;
    }

    @Override
    public boolean handlePaySchNosAndInsertVfInfo(List<String> paySchNoList, Long crtUsrId) throws BaseException {
        return assVfInfoService.handlePaySchNosAndInsertVfInfo(paySchNoList, crtUsrId);
    }

    @Override
    public Pager searchAssVfRtnLogListPage(Pager page) throws BaseException {
        Pager pager = assVfRtnLogService.searchListPage(page);
        List<AssVfRtnLogDTO> resultList = (List<AssVfRtnLogDTO>) pager.getResultList();
        if (CollectionUtils.isNotEmpty(resultList)) {
            Map<String, String> isAllRtnCdMap = dictionaryApiService.searchGroupToMap(AssVfInfoConstant.SYS_CD, AssVfInfoConstant.ORG_CD, AssVfInfoConstant.IS_ALL_RTN_GRP_CD);
            for (AssVfRtnLogDTO assVfRtnLogDTO : resultList) {
                if (isAllRtnCdMap != null && isAllRtnCdMap.containsKey(assVfRtnLogDTO.getIsAllRtn().toString())) {
                    assVfRtnLogDTO.setIsAllRtnNm(isAllRtnCdMap.get(assVfRtnLogDTO.getIsAllRtn().toString()));
                }
            }
            pager.setResultList(resultList);
        }
        return pager;
    }

    @Override
    public boolean deleteAssVfRtnLogByIds(List<Long> ids) throws BaseException {
        return assVfRtnLogService.removeByIds(ids);
    }

    @Override
    public void addRtnLogs(List<AssVfRtnLogDTO> list) throws BaseException {
        for (AssVfRtnLogDTO assVfRtnLogDTO : list) {
            if (StringUtils.isEmpty(assVfRtnLogDTO.getId())) {
                assVfRtnLogService.add(assVfRtnLogDTO);
            } else {
                assVfRtnLogService.modify(assVfRtnLogDTO);
            }
        }
    }

    @Override
    public AssVfInfoDTO getAssVfInfo(Long infoId) throws BaseException {
        return assVfInfoService.selectById(infoId);
    }

}
